package com.czwj.service.impl;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.czwj.dao.AttachmentMapper;
import com.czwj.dao.AttachmentRelyMapper;
import com.czwj.dao.GoodsMapper;
import com.czwj.dao.ProjectItemChildMapper;
import com.czwj.dao.ProjectItemMapper;
import com.czwj.entities.Attachment;
import com.czwj.entities.AttachmentRely;
import com.czwj.entities.Goods;
import com.czwj.entities.ProjectItem;
import com.czwj.service.GoodsService;

/**
* @ClassName: GoodsServiceImpl
* @Description: TODO
* @author H
* @date 2020年4月24日
*
*/
@Service
public class GoodsServiceImpl implements GoodsService{
	@Resource
	GoodsMapper mapper;
	
	@Resource
	ProjectItemMapper itemmapper;
	
	@Resource
	ProjectItemChildMapper itemchildmapper;
	
	@Resource
	AttachmentMapper amapper;
	
	@Resource
	AttachmentRelyMapper relymapper;
	
	@Value("${resources.phypath}")
	private String PhyPath;
	
	@Value("${resources.respath}")
	private String ResPath;
	
	private final String Attachment_Goods_Path = "物资管理/";
	
	/** 
	* 
	* @param id
	* @param projectitemid
	* @param projectitemchildid
	* @param name
	* @param specs
	* @param unit
	* @param initial
	* @param remark
	* @param status
	* @param createrid
	* @param createip
	* @param images
	* @return
	* @see com.czwj.service.GoodsService#insertorupdate(java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.Integer, int, java.lang.String, org.springframework.web.multipart.MultipartFile[])
	*/
	@Transactional
	@Override
	public int insertorupdate(Integer id,Integer projectitemid,Integer projectitemchildid,String name,String specs,
			String unit,String initial,String remark,String actual,Integer status,int createrid,String createip,
			MultipartFile[] images){
		// TODO Auto-generated method stub
		int count = 0;
		if(id == null){
			try{
				Goods goods = new Goods(id,projectitemid,projectitemchildid,name,specs,unit,initial,initial,
						remark,actual,status,createrid,new Date(),createip);
				count += this.mapper.insertSelective(goods);
				ProjectItem item = this.itemmapper.selectByPrimaryKey(projectitemid);
				String path = item.getName() + "/" + Attachment_Goods_Path + name + "/";
				String sourcepath = PhyPath + path;// 存储路径
				String urlpath = ResPath + path;// 资源路径
				File dest = new File(sourcepath);
				if(!dest.exists()){
					dest.mkdirs();
				}
				for(MultipartFile image:images){
					String filename = image.getOriginalFilename();
					if(!filename.contains(".")) {
						filename+=".jpg";
					}
					String _sourcepath = sourcepath + filename;
					String _urlpath = urlpath + filename;
					File file = new File(_sourcepath);
					while(file.exists()){
						String[] arr = _sourcepath.split("\\.");
						_sourcepath = "";
						for(int i = 0;i < arr.length;i++){
							if(i < arr.length - 1){
								_sourcepath += arr[i];;
							}else{
								_sourcepath += "(1)." + arr[i];
							}
						}
						file = new File(_sourcepath);
					}
					image.transferTo(new File(_sourcepath));
					Attachment attachment = new Attachment(filename,_urlpath,_sourcepath,"image",createrid,new Date(),
							createip);
					count += this.amapper.insertSelective(attachment);
					AttachmentRely aRely = new AttachmentRely("goods",goods.getId(),attachment.getId());
					count += this.relymapper.insertSelective(aRely);
				}
			}
			catch(Exception e){
				// TODO Auto-generated catch block
				return 0;
			}
		}else{
			try{
				Goods goods = new Goods(id,projectitemid,projectitemchildid,name,specs,unit,initial,
						remark,actual,status,createrid,new Date(),createip);
				count += this.mapper.updateByPrimaryKeySelective(goods);
				ProjectItem item = this.itemmapper.selectByPrimaryKey(projectitemid);
				String path = item.getName() + "/" + Attachment_Goods_Path + name + "/";
				String sourcepath = PhyPath + path;// 存储路径
				String urlpath = ResPath + path;// 资源路径
				File dest = new File(sourcepath);
				if(!dest.exists()){
					dest.mkdirs();
				}
				for(MultipartFile image:images){
					String filename = image.getOriginalFilename();
					if(!filename.contains(".")) {
						filename+=".jpg";
					}
					String _sourcepath = sourcepath + filename;
					String _urlpath = urlpath + filename;
					File file = new File(_sourcepath);
					while(file.exists()){
						String[] arr = _sourcepath.split("\\.");
						_sourcepath = "";
						for(int i = 0;i < arr.length;i++){
							if(i < arr.length - 1){
								_sourcepath += arr[i];;
							}else{
								_sourcepath += "(1)." + arr[i];
							}
						}
						file = new File(_sourcepath);
					}
					image.transferTo(new File(_sourcepath));
					Attachment attachment = new Attachment(filename,_urlpath,_sourcepath,"image",createrid,new Date(),
							createip);
					count += this.amapper.insertSelective(attachment);
					AttachmentRely aRely = new AttachmentRely("goods",goods.getId(),attachment.getId());
					count += this.relymapper.insertSelective(aRely);
				}
			}
			catch(Exception e){
				// TODO Auto-generated catch block
				return 0;
			}
		}
		return count;
	}
	
	/** 
	* 
	* @param id
	* @return
	* @see com.czwj.service.GoodsService#delete(int)
	*/
	@Override
	public int delete(int id){
		// TODO Auto-generated method stub
		return this.mapper.deleteByPrimaryKey(id);
	}
	
	/** 
	* 
	* @param map
	* @return
	* @see com.czwj.service.GoodsService#search(java.util.Map)
	*/
	@Override
	public List<Goods> search(Map<String,Object> map){
		// TODO Auto-generated method stub
		return this.mapper.search(map);
	}
	
	/** 
	* 
	* @param map
	* @return
	* @see com.czwj.service.GoodsService#count(java.util.Map)
	*/
	@Override
	public int count(Map<String,Object> map){
		// TODO Auto-generated method stub
		return this.mapper.count(map);
	}

	/** 
	* 
	* @param id
	* @return
	* @see com.czwj.service.GoodsService#selectByPrimaryKey(int)
	*/
	@Override
	public Goods selectByPrimaryKey(int id){
		// TODO Auto-generated method stub
		return this.mapper.selectByPrimaryKey(id);
	}

	/** 
	* 
	* @param goods
	* @see com.czwj.service.GoodsService#update(com.czwj.entities.Goods)
	*/
	@Override
	public int update(Goods goods){
		return this.mapper.updateByPrimaryKeySelective(goods);
	}

	/** 
	* 
	* @param map
	* @return
	* @see com.czwj.service.GoodsService#searchbylevel2(java.util.Map)
	*/
	@Override
	public List<Goods> searchbylevel2(Map<String,Object> map){
		// TODO Auto-generated method stub
		return this.mapper.searchbylevel2(map);
	}

	/** 
	* 
	* @param map
	* @return
	* @see com.czwj.service.GoodsService#countbylevel2(java.util.Map)
	*/
	@Override
	public int countbylevel2(Map<String,Object> map){
		// TODO Auto-generated method stub
		return this.mapper.countbylevel2(map);
	}

	/** 
	* 
	* @param map
	* @return
	* @see com.czwj.service.GoodsService#searchReviewedLevel2(java.util.Map)
	*/
	@Override
	public List<Goods> searchReviewedLevel2(Map<String,Object> map){
		// TODO Auto-generated method stub
		return this.mapper.searchReviewedLevel2(map);
	}

	/** 
	* 
	* @param map
	* @return
	* @see com.czwj.service.GoodsService#countReviewedLevel2(java.util.Map)
	*/
	@Override
	public int countReviewedLevel2(Map<String,Object> map){
		// TODO Auto-generated method stub
		return this.mapper.countReviewedLevel2(map);
	}
}
